/*
 * pH Javascript
 */

var pH = 
{
	VERSION: '1.0',
	url: '',
	jquery_url: 'http://jqueryjs.googlecode.com/files/',
	jquery_ui_url: 'http://jquery-ui.googlecode.com/svn/tags/1.6rc5/',

	theme_url: 'http://jquery-ui.googlecode.com/svn/tags/1.6rc5/',

	log: function(message)
	{
		try {
			console.log(message);
		} catch(e) {};
	},

	info: function(message)
	{
		try {
			console.info(message);
		} catch(e) {};
	},

	require_file: function(file, where, on_ready)
	{
		var head = document.getElementsByTagName('head').item(0);
		var script = document.createElement('script');
		script.setAttribute('language', 'javascript');
		script.setAttribute('src', where+file);
		script.setAttribute('type', 'text/javascript');
		script.onload = on_ready;

		head.appendChild(script);
	},

	require: function(file, where, on_ready)
	{
		if (where == undefined) where = pH.url + '/js/';

		if (typeof(file) == 'object')
			for (i in file)
				this.require_file(file[i], where);
		else
			this.require_file(file, where, on_ready);
	},

	/*
	 * Loads Javascript file using JQuery Base URL
	 */
	require_jquery: function(file, on_ready)
	{
		this.require(file, this.jquery_url, on_ready);
	},

	require_jquery_ui: function(file, on_ready)
	{
		this.require(file, this.jquery_ui_url, on_ready);
	},

	require_css: function(css)
	{
		var head = document.getElementsByTagName('head').item(0);
		var link  = document.createElement('link');

		link.setAttribute('href', css);
		link.setAttribute('rel', 'stylesheet');
		link.setAttribute('type', 'text/css');

		head.appendChild(link);
	},

	theme: function(theme_name)
	{
		if (theme_name == undefined)
			theme_name = 'themes/base/ui.all.css';

		this.require_css(this.theme_url + theme_name);
	}

}

// load jQuery
//pH.require_jquery('jquery-1.3.1.min.js', function()
{
	jQuery.fn.pH_container = function()
	{
		var create_div = function(class_name)
		{
			var x = document.createElement("DIV");
			x.className = class_name;

			return x;
		};

		return this.each(function() {
			var b = create_div("ph-body");
			b.innerHTML = this.innerHTML;
			this.innerHTML = "";

			this.appendChild(create_div("ph-head"));
			this.appendChild(b);
			this.appendChild(create_div("ph-foot"));
		});
	};

	/**
	 * Gets Body of pH Container
	 */
	jQuery.fn.pH_body = function()
	{
		var result = this.children(".ph-body");
		if (result.length == 0)
			result = this;
		return result;

	};

	jQuery.fn.pH_head = function()
	{
		var result = this.children(".ph-head");
		if (result.length == 0)
			result = this;
		return result;
	};

	jQuery.fn.pH_foot = function()
	{
		var result = this.children(".ph-foot");
		if (result.length == 0)
			result = this;
		return result;
	};
}

pH.require_jquery_ui('ui/ui.core.js');
pH.info('pH+ Successfully Enabled.');
